<?php


namespace models\Business;


use models\Exception\DAO\ModelDriverException;
use models\Exception\DAO\ModelException;
use models\Exception\DAO\ModelReflectionException;
use models\Exception\DAO\ModelSqlException;
use models\Exception\Service\ServiceException;
use models\Exception\Transformer\TransformerException;
use models\Service\AlibabashippingService;

class AlibabaShipping
{
    /**
     * 将1688商品物流信息写入数据库
     * @param array $parameters
     * @throws ModelDriverException
     * @throws ModelException
     * @throws ModelReflectionException
     * @throws ModelSqlException
     * @throws ServiceException
     * @throws TransformerException
     */
    static public function collectShipping($parameters = []) {
        try {
            // 获取attribute_id数组
            $product_id_arr = array_column($parameters, 'product_id');
            $product_id_arr = array_filter(array_unique($product_id_arr));

            // 查询attribute_id是否存在于数据库
            if (count($product_id_arr) == 1) {
                $shipping_exists = AlibabashippingService::getInstance()->getList(1, 9999, 'desc', 'id', ['groupOp' => 'product_id', 'rules' =>
                    [
                        ['field' => 'product_id', 'op' => 'eq', 'data' => implode(',', array_values($product_id_arr))]
                    ]]);
            } else {
                $shipping_exists = AlibabashippingService::getInstance()->getList(1, count($product_id_arr), 'desc', 'product_id',
                    ['groupOp' => 'AND', 'rules' => [
                        ['field' => 'product_id', 'op' => 'in', 'data' => implode(',', $product_id_arr)]]]);
            }

            $batch_update_arr = $batch_insert_arr = [];
            // attribute存在
            if (!empty($shipping_exists['data'])) {
                $exists_product_id_arr = array_column($shipping_exists['data'], 'to_area_code_text');
                $exists_shipping_id_arr = array_column($shipping_exists['data'], 'id');
                foreach ($parameters as $shipping_info) {
                    // 已存在的商品写入更新数据
                    if (in_array($shipping_info['to_area_code_text'], $exists_product_id_arr)) {
                        if (false !== $idx = array_search($shipping_info['to_area_code_text'], $exists_product_id_arr)) {
                            $shipping_info['id'] = $exists_shipping_id_arr[$idx];
                            array_push($batch_update_arr, $shipping_info);
                        }
                    }
                    // 不存在的商品写入插入数组
                    else
                        array_push($batch_insert_arr, $shipping_info);
                }
            } else {
                $batch_insert_arr = $parameters;
            }

            // 更新数组存在
            if (!empty($batch_update_arr)) {
                // 更新数组长度大于1，批量插入
                if (count($batch_update_arr) > 1)
                    AlibabashippingService::getInstance()->batchUpdate($batch_update_arr);
                // 更新数组长度等于1，单条更新
                else {
                    $update_arr = array_values($batch_update_arr);
                    $update_arr = $update_arr[0];
                    $product_id = $update_arr['product_id'];
                    AlibabashippingService::getInstance()->update($update_arr, $product_id);
                }
            }

            // 插入数据存在
            if (!empty($batch_insert_arr))
                // 批量插入数据
                AlibabashippingService::getInstance()->batchInsert($batch_insert_arr);

        } catch (ModelDriverException $e) {
            throw $e;
        } catch (ModelException $e) {
            throw $e;
        } catch (ModelReflectionException $e) {
            throw $e;
        } catch (ModelSqlException $e) {
            throw $e;
        } catch (ServiceException $e) {
            throw $e;
        } catch (TransformerException $e) {
            throw $e;
        }

    }

}
